<%@ page language="java" pageEncoding="utf-8" %>

<!DOCTYPE html>
<html>
<head>
    <%@include file="../common/base.jsp"%>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1"/>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache,must-revalidate">
    <meta http-equiv="expires" content="0">
    <title>行程中</title>
    <link rel="stylesheet" type="text/css" href="${base}/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="${base}/css/global.css?version=${version}">
    <link rel="stylesheet" type="text/css" href="${base}/css/takecar.css?version=${version}">
    <style type="text/css">
        body {
            background-color: #fff;
        }

        .divBox {
            width: 100%;
            border: none;
            box-shadow: none;
            margin-top: 0px;
        }

        .paymoney {
            border-bottom: 1px solid #f0f2f4;
        }

        .wechatPay .img {
            width: 15px;
            height: 15px;
            margin-top: 5px;
        }

        .cancelPaybtn {
            color: #9ea7b1;
            text-decoration: none;
        }
    </style>
</head>
<body>
<div class="container-fluid">
    <div class="divBox router-info">
        <p class="text-center clearfix">
            <a class="pull-left cancelPaybtn" href="#">取消</a>
            <span>支付</span>
        </p>
        <div class="paymoney">
            <p class="text-center">
                共<span style="font-size: 26px;">${sessionScope.totalFare}</span>元
            </p>
            <div class="clearfix youhui">
                <span class="pull-left" style="color:#f99359;"></span>
                <span class="pull-right" style="color: #4f5a67;"></span>
            </div>
        </div>
        <p class="wechatPay clearfix">
            <img class="img pull-left" src="${base}/images/check.png">
            <span style="color: #4f5a67;">微信支付</span>
            <img class="img pull-right" src="${base}/images/check.png">
        </p>
    </div>

    <a id="btn_wx_pay" class="btn-pay" href="#">
        确认支付
    </a>
</div>
</body>
<script type="text/javascript" src="${base}/js/jquery.min.js"></script>
<script type="text/javascript" src="${base}/js/localStorageOpt.js?version=${version}"></script>

<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<%--<script type="text/javascript" src="${base}/plugin/jsencrypt/jsencrypt.min.js"></script>--%>
<!-- 获取IP地址工具-->
<script src="https://pv.sohu.com/cityjson?ie=utf-8"></script>

<script type="text/javascript">


    /*var orderUuid = "${orderUuid}";
     var couponUuid = "";
     var openid = "${openId}";
     var spbillCreateIp = location.href.split('#')[0];
     console.log(spbillCreateIp)
     $.post("/wx/tradeUrl", {
     orderUuid: orderUuid,
     couponUuid: couponUuid,
     openid: openid,
     spbillCreateIp: spbillCreateIp
     }, function (result) {
     console.log(result)
     if (result.success) {
     var data = result.data;
     setLocalStorage('wxData', data);
     wx.config({
     debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
     appId: data.appid, // 必填，公众号的唯一标识
     timestamp: data.timestamp, // 必填，生成签名的时间戳
     nonceStr: data.noncestr, // 必填，生成签名的随机串
     signature: data.sign,// 必填，签名，见附录1
     jsApiList: ['checkJsApi', 'chooseWXPay'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
     });
     }
     });

     wx.ready(function () {

     });

     wx.error(function (res) {
     alert(res);
     // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
     });

     document.querySelector('#btn_wx_pay').onclick = function () {
     var data = getLocalStorage('wxData', 'object');
     console.log(data)
     wx.checkJsApi({
     jsApiList: ['chooseWXPay'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
     success: function (res) {
     alert('success')
     // 以键值对的形式返回，可用的api值true，不可用为false
     // 如：{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
     },
     error: function (res) {
     alert('error')
     }
     });
     wx.chooseWXPay({
     timestamp: data.timestamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
     nonceStr: data.noncestr, // 支付签名随机串，不长于 32 位
     package: "prepay_id=" + data.prepayid, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）
     signType: 'MD5', // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
     paySign: data.sign, // 支付签名
     success: function (res) {
     // 支付成功后的回调函数
     alert('success!!')
     }
     });
     };

     document.querySelector('.cancelPaybtn').onclick = function () {
     // 关闭窗口
     var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
     parent.layer.close(index);
     };*/

    var curIp = returnCitySN["cip"]; // 当前IP地址
    alert(curIp)

    var orderUuid = "${orderUuid}";
    var couponUuid = "";
    var openid = "${openId}" || getLocalStorage('openId');
//    var spbillCreateIp = '192.168.1.1';
    var p={
        orderUuid: orderUuid,
        couponUuid: couponUuid,
        openid: openid,
        spbillCreateIp: curIp
    };
    $.post("${base}/wx/tradeUrl",
        {params: (JSON.stringify(p)).encryptRSA()},
        function (result) {
            result=result.decryptRSA()
            if (result.success) {
                var data = result.data;
                setLocalStorage('wxData', data);
            }
        }
    );

    function onBridgeReady() {
        var data = getLocalStorage('wxData', 'object')
        alert(typeof(WeixinJSBridge))
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest', {
                "appId": data.appid,     //公众号名称，由商户传入
                "timeStamp": data.timestamp,         //时间戳，自1970年以来的秒数
                "nonceStr": data.noncestr, //随机串
                "package": "prepay_id=" + data.prepayid,
                "signType": "MD5",         //微信签名方式
                "paySign": data.sign //微信签名
            },
            function (res) {
                alert(res)
                if (res.err_msg == "get_brand_wcpay_request:ok") {
                }     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
            }
        );
    }

    function doWxPay() {
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
            }
        } else {
            onBridgeReady();
        }
    }


    document.querySelector('#btn_wx_pay').onclick = function () {

        doWxPay();
    };

    document.querySelector('.cancelPaybtn').onclick = function () {
        // 关闭窗口
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        parent.layer.close(index);
    };


</script>
</html>